/* NoteTaker
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Enters notes and appends them to a notes.txt file for consumption by GeekTool. A note
beginning with a space is appended to the previous notes. A note beginning with "REM"
is appended to the ".reminders" file for the remind console application.

NOTE: If you want to use the output to your ./reminders file, and if your user name
is not "michael", you will need to modify the Write() function below. If you are not
using remind, remove the "REM" clause from Write().
*/

#include "_sdl/Window.h"
#include "_paste/Pasteboard.h"
#include "_sdl/Font.h"
#include "_sdl/EmbeddedFont.h"
#include "_common/shift.h"
#include "note.h"

#include "SDL.h"

#include <string>
#include <fstream>
#include <iostream>

using std::string;
using std::ios;
using std::ofstream;
using std::endl;
using std::ifstream;
using std::getline;
using std::cerr;



void Run(string path);
void Draw(Window &window, Font &font, const string &text, int cursor);

int EventFilter(const SDL_Event *event)
{
	return (event->type == SDL_QUIT) | (event->type == SDL_KEYDOWN);
}




int main(int argc, char *argv[])
{
	Run(argv[0]);

	SDL_Quit();
	return 0;
}



void Run(string path)
{
	// Initialize SDL.
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		cerr << "Unable to initialize SDL." << endl;
		return;
	}

	Font font;
	font.Load(EmbeddedFont::DATA, EmbeddedFont::WIDTH, EmbeddedFont::HEIGHT);
	
	Window window;
	window.SetCaption("NoteTaker");
	window.Create(512, 18, SDL_SWSURFACE | SDL_NOFRAME);
	font.SetColor(255, 255, 255);

	SDL_SetEventFilter(&EventFilter);
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
	
	string text;
	int cursor = 0;
	
	SDL_Event event;
	while(true)
	{
		Draw(window, font, text, cursor);
		SDL_WaitEvent(&event);
		
		if(event.type == SDL_QUIT)
			break;
		else if(event.type == SDL_KEYDOWN)
		{
			SDLKey key = event.key.keysym.sym;
			SDLMod mod = event.key.keysym.mod;
			
			// Get the modifier key state.
			bool isCommand = (mod & (KMOD_CTRL | KMOD_META)) != 0;
			bool isShifted = (mod & KMOD_SHIFT) != 0;
			
			// Escape or ^Q quits.
			if(key == SDLK_ESCAPE || (isCommand && key == 'q'))
				break;
			else if(isCommand && key == 'c')
				Pasteboard::Set(note_read());
			else if(isCommand && key == 'x')
			{
				Pasteboard::Set(text);
				text.clear();
				cursor = 0;
			}
			else if(isCommand && key == 'v')
			{
				string clip = Pasteboard::Get();
				text.insert(cursor, clip);
				cursor += clip.length();
			}
			else if(key == SDLK_RETURN)
			{
				note_write(text);
				// Update notes, and quit.
				break;
			}
			else if(key == SDLK_LEFT && cursor)
				--cursor;
			else if(key == SDLK_RIGHT && cursor < static_cast<int>(text.length()))
				++cursor;
			else if((key == SDLK_DELETE || key == SDLK_BACKSPACE) && cursor)
			{
				--cursor;
				text.erase(cursor, 1);
			}
			else if(!isCommand && key >= ' ' && key <= '~')
			{
				text.insert(cursor, 1, isShifted ? SHIFT[key] : key);
				++cursor;
			}
		}
	}
}



void Draw(Window &window, Font &font, const string &text, int cursor)
{
	window.Frame(0, 0, window.Width(), window.Height(), window.Color(0));
	
	window.FillRect(1, 1, window.Width() - 2, window.Height() - 2, window.Color(90, 40, 20));
	
	int cursorX = font.Width(text.begin(), text.begin() + cursor);
	window.VLine(1 + cursorX, 1, 16, window.Color(220, 140, 70));
	
	font.Draw(text, window, 1, 1);
	
	window.Update();
}



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
